<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Listing extends MX_Controller {


	function __construct()
	{
		parent::__construct();
		$this->load->library('pagination');
		$this->load->model('location/location_model');
		$this->load->model('category/category_model');
		$this->load->model('listing/listing_model');

	}


	function show($para='CATEGORY', $pID=0, $per_page=5,$featured=FALSE,$offset=0){

		$data['Page'] ='LISTING';
		$sql='';
		if($para == 'CATEGORY'){

			$id = $this->commoncall->get_id(current_url());
			
			
			
			$seg_cat = $this->uri->segment(3);
			
			$key_id = $this->commoncall->get_id($seg_cat);
			
			$offset = 0 + $this->uri->segment(4);
			
			$total = $this->listing_model->count_all_listing($key_id);
			
			//print $total;
			//die();
			
			$perpage = 5;
			$config['per_page'] = $perpage;
			$config['total_rows'] = $total;
			$config['uri_segment'] = '4';
			
			$config['base_url'] = base_url() . 'browse/practice/'.$seg_cat;
			
			
			
			$cur =base_url()."browse/practice/'.$seg_cat";
			$cur = '<a href="'. $cur   .'">&laquo; First</a>';
			$config['full_tag_open'] = '<ul class="pagination">';
			$config['full_tag_close'] = '</ul><!--pagination-->';
			$config['first_link'] = '&laquo; First';
			$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['first_tag_close'] = '</li>';
			$config['last_link'] = 'Last &raquo;';
			$config['last_tag_open'] = '<li class="next page" style="display:inline">';
			$config['last_tag_close'] = '</li>';
			$config['next_link'] = 'Next &rarr;';
			$config['next_tag_open'] = '<li class="next page" style="display:inline">';
			$config['next_tag_close'] = '</li>';
			$config['prev_link'] = '&larr; Previous';
			$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['prev_tag_close'] = '</li>';
			$config['cur_tag_open'] = '<li class="active"><a href="">';
			$config['cur_tag_close'] = '</a></li>';
			$config['num_tag_open'] = '<li class="page">';
			$config['num_tag_close'] = '</li>';
			$config['display_pages'] = FALSE;
			
			$this->pagination->initialize($config);
			
			
			
			
			$cityID = 0;

			if($this->input->cookie('cook_city')){
				$cityID = $this->input->cookie('cook_city');
			}


			if($cityID !=0){
				
				$sql = "SELECT t1.* FROM profile AS t1
				LEFT JOIN term AS t2 ON t1.id = t2.listing_id
				WHERE t1.status =  51 and t1.city = $cityID and t2.term_value = $pID and featured = 0 LIMIT $offset, $per_page ";
				
			}else{
				$sql = "SELECT DISTINCT t1.title, t1.* FROM profile AS t1
				INNER JOIN term AS t2 ON t1.id = t2.listing_id
				WHERE t2.term =  'key'
				AND t2.term_value =  $pID and featured = 0 LIMIT $offset, $per_page" ;
			}

			
			
		}
		if($para == 'REGION'){
			$sql = "SELECT DISTINCT t1.title,t1.* FROM profile AS t1
			INNER JOIN term AS t2 ON t1.id = t2.listing_id
			WHERE t1.state = $pID LIMIT $offset, $per_page" ;
		}

		if($para == 'CITY'){
			$sql = "SELECT DISTINCT t1.title,t1.* FROM profile AS t1
			INNER JOIN term AS t2 ON t1.id = t2.listing_id
			WHERE t1.city = $pID LIMIT $offset, $per_page" ;
		}
			
			
			
		
		
		//print $sql;
		//-- GET LISTING FROM TERM --//
		$data['listing'] = $result = $this->db->query($sql)->result();;


		
		
		
		
		$this->load->view('listing',$data);

	}






	function category($catID=0,$cityID=0){

		$data['Page'] ='LISTING';
		$limit=10;

		if($this->uri->segment(4)){
			$offset = $this->uri->segment(4);
		}else{
			$offset = 0;
		}

		//-- GET LISTING FROM TERM --//
		$data['listing'] = $this->listing_model->getListing($catID,$limit=$limit,$offset=$offset);




		/* META */
		$data['site_title'] = $this->settings_model->siteInfo('web_title');
		$data['canonical'] = base_url();
		$this->template->title($data['site_title']);
		$this->template->set_metadata('keywords', $this->settings_model->siteInfo('keyword'));
		$this->template->set_metadata('description', $this->settings_model->siteInfo('description'));

		$this->template->build('index',$data);
	}




	public function category_wise($catID=0,$cityID=0)
	{

		$data['Page'] ='LISTING';
		$limit=10;


		if($this->uri->segment(4)){
			$offset = $this->uri->segment(4);
		}else{
			$offset = 0;
		}


		//-- GET LISTING FROM LISTING KEYWORD --//
		$data['listing'] = $this->listing_model->getListing($catID,$limit=$limit,$offset=$offset);

		//--GET DYNAMIC LINK --//
		$data['link'] = $this->commoncall->url($this->category_model->getCategory_info($catID)->name);
		$data['tag'] = $this->category_model->getCategory_info($catID)->name;

		//--PAGINATION --//
		$config['base_url'] = base_url().'browse/category/'.$data['link'].'-'.$catID;
		$config['total_rows'] = 500;
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;

		$config['display_pages'] = FALSE;
		$config['first_link'] = TRUE;
		$config['last_link'] = TRUE;

		$config['full_tag_open'] = '<ul class="pager pull-right">';
		$config['full_tag_close'] = '</ul><!--pagination-->';

		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page">';
		$config['first_tag_close'] = '</li>';

		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page">';
		$config['last_tag_close'] = '</li>';

		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page">';
		$config['next_tag_close'] = '</li>';

		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page">';
		$config['prev_tag_close'] = '</li>';


		$this->pagination->initialize($config);

		$data['pagination'] = $this->pagination->create_links();

		$this->benchmark->mark('code_end');

		$data['ellapse_time'] = $this->benchmark->elapsed_time('code_start', 'code_end');

		$this->load->view('listing',$data);
	}

	function lawyer()
	{
		$lawyer = trim($this->input->post('lawyer_search'));


		$sql = "SELECT * FROM profile WHERE status = 51 ";

		if($_REQUEST['city_id'] != ''){
			$city_id = $_REQUEST['city_id'];
			$sql .= "AND city = $city_id ";
		}

		if($lawyer != ''){
			$sql .= " AND title LIKE '" . mysql_real_escape_string($lawyer) . "%' ";
		}

		//--Limit --//
		$sql .= "LIMIT 0, 20 ";

		//print $sql;
		//die();

		$result=$this->db->query($sql)->result();

		$data['listing']['all'] = $result;


		$data['title'] = $this->input->post('lawyer_search');

		$title = $data['title'];
		
		$this->template->set_breadcrumb('Home', '/')
		//->set_breadcrumb('Category','')
		->set_breadcrumb('Category','category/all')
		->set_breadcrumb($title,'');

		$this->template->set_partial('nav_bar','header/nav_bar');
		$this->template->set_partial('header_search','header/header_search');

		$this->template->set_layout('details_layout');
		$this->template->build('lawyer_search',$data);

	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
